package com.cn.goodsadmin.service;

import com.cn.goodsadmin.common.PageQueryResult;
import com.cn.goodsadmin.dao.DestDTO;
import com.cn.goodsadmin.po.FareTemlatesPageInfo;
import com.cn.goodsadmin.po.FareTemplateInfo;
import com.cn.goodsadmin.po.Faredes;
import com.cn.goodsadmin.po.Faretemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service("FareTemplateService")
public class FareTemplateService extends BaseService {

    public void addFareTemplate(FareTemplateInfo fareTemplateInfo) throws Exception {
        List<Faredes> faredesList = fareTemplateInfo.getFaredesList();
        Faretemplate faretemplate = fareTemplateInfo.getFaretemplate();
        fareTemplateDao.save(faretemplate);
        int id = faretemplate.getcId();
        for (Faredes faredes : faredesList) {
            faredes.setcTempid(id);
            faredesDao.save(faredes);
        }
    }

    public void updateFareTemplate(FareTemplateInfo fareTemplateInfo) throws Exception {
        List<Faredes> faredesList = fareTemplateInfo.getFaredesList();
        Faretemplate faretemplate = fareTemplateInfo.getFaretemplate();
        fareTemplateDao.update(faretemplate);
        //先删除
        List<Faredes> oldFareDes = faredesDao.findAllFareDes(faretemplate.getcId());
        for (Faredes faredes : oldFareDes) {
            faredesDao.delete(faredes);
        }
        //再添加
        int id = faretemplate.getcId();
        for (Faredes faredes : faredesList) {
            faredes.setcTempid(id);
            faredesDao.save(faredes);
        }
    }

    public void deleteFareTemplate(FareTemplateInfo fareTemplateInfo) throws Exception {
        List<Faredes> faredesList = fareTemplateInfo.getFaredesList();
        Faretemplate faretemplate = fareTemplateInfo.getFaretemplate();
        fareTemplateDao.delete(faretemplate);
        for (Faredes faredes : faredesList) {
            faredesDao.delete(faredes);
        }
    }

    public FareTemlatesPageInfo findFareTempInfoByAppid(int pageindex, int pagesize, int appid) throws Exception {
        PageQueryResult pageQueryResult = fareTemplateDao.findFareTemplateById(pageindex, pagesize, appid);
        List<DestDTO> faretemplates = pageQueryResult.getQueryResult();
        FareTemlatesPageInfo pageInfo = new FareTemlatesPageInfo();
        pageInfo.pageIndex = pageQueryResult.getPageIndex();
        pageInfo.pagesize = pageQueryResult.getPageSize();
        pageInfo.pagecount = pageQueryResult.getPageCount();
        pageInfo.totalcount = pageQueryResult.getTotalCount();
        if (faretemplates != null && faretemplates.size() > 0) {
            List<FareTemplateInfo> fareTemplateInfos = new ArrayList<FareTemplateInfo>();
            for (DestDTO destDTO : faretemplates) {
                FareTemplateInfo fareTemplateInfo = new FareTemplateInfo();
                fareTemplateInfo.setFaretemplate(destDTO.getFaretemplate());
                int tid = destDTO.getFaretemplate().getcId();
                List<Faredes> faredes = faredesDao.findAllFareDes(tid);
                fareTemplateInfo.setFaredesList(faredes);
                fareTemplateInfos.add(fareTemplateInfo);
            }
            pageInfo.fareTemplateInfos = fareTemplateInfos;
        }
        return pageInfo;
    }
}
